package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JOptionPane;
import javax.swing.text.JTextComponent;

import view.ComposeMailMainMenu;
import view.ComposeMailView;
import view.EmailEditorPanel;



/**
 * Controller for compose email view
 * @author Farzana Alam (farzana22@gmail.com)
 * @since May 2013
 */
public class ComposeMailController{	
	
	// View to control
	private ComposeMailView view;
	
	// Components under the View to control
	private EmailEditorPanel editorPanel;
	private ComposeMailMainMenu mainMenu;
	
	// Controllers
	private EmailEditorController editorController;	
	
	// Texts for the Text Fields
	private String _fromAddressText, _toAddressText, _subjectFieldText, _bodyText;
		
//	 Email state
//	private EmailState emailState;
	
	// Last focused textField
	private JTextComponent lastFocusedTF;
	
	/**
	 * 
	 */
	public ComposeMailController() {
		
		view = new ComposeMailView();
		mainMenu = view.getMainMenu();
		editorPanel = view.getEmailEditorPanel();
		editorController = new EmailEditorController();
		new ComposeMainMenuController();
		
		_fromAddressText = "farzana22@gmail.com";
		editorController.set_fromAddressText(_fromAddressText);
		
		assignHandlersToEvent();
	}

	/**
	 * 
	 */
	private void assignHandlersToEvent() {
		
		// Window closing event
		view.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				actionWindowClosing();
			}			
			
		});
		
	}
	
	private void actionWindowClosing() {
		Object[] options = {"Save", "Don't Save"};		
		int n = JOptionPane.showOptionDialog(null, "Message has not been sent. Do you want to save "
					+ "the message in the Drafts folder?", "Save Message", JOptionPane.YES_NO_OPTION,
					JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
		
		switch (n) {
		
		case JOptionPane.YES_OPTION:
			editorController.actionSave();			
			break;
		
		case JOptionPane.NO_OPTION:
			view.dispose();

		default:
			break;
		} // close switch statement
	} // close actionWnidowClosing()
	
	/**
	 * 
	 * @author Farzana Alam (farzana22@gmail.com)
	 * @since May 2013
	 *
	 */
	private class EmailEditorController {
		
		private EmailEditorController() {
			assignListenersToButtons();
			assignFocusListenersToTF();
			
		}
		
		private void assignFocusListenersToTF() {
			
			editorPanel.get_toAddress().addFocusListener(new FocusListener() {
				
				@Override
				public void focusLost(FocusEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void focusGained(FocusEvent e) {
					lastFocusedTF = editorPanel.get_toAddress();
				}
			});
			
			editorPanel.get_subjectField().addFocusListener(new FocusListener() {
				
				@Override
				public void focusLost(FocusEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void focusGained(FocusEvent e) {
					lastFocusedTF = editorPanel.get_subjectField();
				}
			});
			
			editorPanel.get_body().addFocusListener(new FocusListener() {
				
				@Override
				public void focusLost(FocusEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void focusGained(FocusEvent e) {
					lastFocusedTF = editorPanel.get_body();
				}
			});
		}

		private void assignListenersToButtons() {
			/*
			 * 
			 */
			
			editorPanel.getSendButton().addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					actionSend();
				}
			});
			
			editorPanel.getSaveButton().addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					actionSave();
				}
			});
			
		}

		private void actionSend() {
			/*
			 * Send the email and close the ComposeMailView Mail View
			 */
			
			// Send the email
			getTextFromFields();
			
			
			// Close the view		
			closeView();
		}
		
		public void actionSave() {
			/*
			 * Save the email to ToSend folder and close the view
			 */
			
			// Save the email
			getTextFromFields();		
//			testSaveMail();
			
			
			// Close the view
			closeView();
		}
		
		private void getTextFromFields() {
			/*
			 * Get the texts from the text fields 
			 */
			
			_toAddressText = editorPanel.get_toAddress().getText();
			_subjectFieldText = editorPanel.get_subjectField().getText();
			_bodyText = editorPanel.get_body().getText();
		}	

		/*// testing String objects
		private void testSaveMail() {
			System.out.println("From: " + _fromAddressText + "\n" + "To: " + _toAddressText + "\n"
									+ "Subject: " + _subjectFieldText + "\n" + "Body: " + _bodyText);
		}*/

		// Setting From field in the editor with respective email address
		public void set_fromAddressText(String _fromAddressText) {
			editorPanel.get_fromAddress().setText(_fromAddressText);
		}
		
		// Close the Compose view
		public void closeView() {
			view.setVisible(false);
			view.dispose();
		}
		
	}
	
	/**
	 * 
	 * @author Farzana Alam (farzana22@gmail.com)
	 * @since May 2013
	 *
	 */
	private class ComposeMainMenuController {
		/**
		 * 
		 */
		
		/*
		 * 
		 */
		private ComposeMainMenuController() {
			addActionListeners();
		}
		
		/*
		 * 
		 */
		private void addActionListeners() {
			addCopyMenuListener();
			addCutMenuListener();
			addPasteMenuListener();
			addSelectAllMenuListener();
			addSaveAsDraftMenuListener();
			addNewMessageMenuListener();
			addSendLaterMenuListener();
			addSendNowMenuListener();
			addCloseMenuListener();
		}

		private void addCloseMenuListener() {
			mainMenu.getCloseWindow().addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					actionWindowClosing();
				}
			});
			
		}

		private void addSendNowMenuListener() {
			mainMenu.getSendMailNow().addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					editorController.actionSend();
				}
			});
		}

		private void addSendLaterMenuListener() {
			mainMenu.getSendMailLater().addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					editorController.actionSave();
				}
			});
		}

		private void addNewMessageMenuListener() {
			mainMenu.getNewMessage().addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					new ComposeMailController();
				}
			});
		}

		private void addSaveAsDraftMenuListener() {
			mainMenu.getSaveMail().addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					editorController.actionSave();
				}
			});
		}

		private void addSelectAllMenuListener() {
			mainMenu.get_selectAll().addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					lastFocusedTF.selectAll();
				}
			});
		}
		
		private void addPasteMenuListener() {
			mainMenu.get_paste().addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {					
					lastFocusedTF.paste();
				}
			});
		}

		/*
		 * 
		 */
		private void addCutMenuListener() {
			mainMenu.get_cut().addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					getSelectedTextfield().cut();
				}
			});
		}

		/*
		 * 
		 */
		private void addCopyMenuListener() {
			mainMenu.get_copy().addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent arg0) {
					getSelectedTextfield().copy();
				}
			});
			
		}

		/*
		 * 
		 */
		private JTextComponent getSelectedTextfield() {
			if(editorPanel.get_fromAddress().getSelectedText() != null) {
				return editorPanel.get_fromAddress();
			} else if(editorPanel.get_toAddress().getSelectedText() != null) {
				return editorPanel.get_toAddress();
			} else if(editorPanel.get_subjectField().getSelectedText() != null) {
				return editorPanel.get_subjectField();
			} else if(editorPanel.get_body().getSelectedText() != null) {
				return editorPanel.get_body();
			} else {
				return null;
			}
		}
		
		
	}
	
}
